﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
#endregion

namespace SimplePong
{
    public class Button : IGraphicsControl
    {
        //Fields
        protected bool pressed;
        protected Rectangle bounds;
        protected Texture2D texture;
        protected Boolean activated;

        public Boolean Activated
        {
            get { return activated; }
            set { activated = value; }
        }

        public Texture2D Textura
        {
            get { return texture; }
            set { texture = value; }
        }

        #region Events
        public event EventHandler Click;
        protected virtual void OnClick(EventArgs e)
        {
            if (Click != null)
                Click.Invoke(this, e);
        }

        public event EventHandler TouchDown;
        protected virtual void OnTouchDown(EventArgs e)
        {
            if (TouchDown != null)
                TouchDown.Invoke(this, e);
        }

        #endregion

        #region Properties
        public virtual Vector2 Position
        {
            get { return new Vector2(bounds.X, bounds.Y); }
            set 
            { 
                bounds.X = (int)value.X;
                bounds.Y = (int)value.Y;
            }
        }



        public virtual Rectangle Bounds
        {
            get { return bounds; }
        }



        #endregion

        #region Initialize
        public Button(Texture2D texture, Rectangle bounds)
        {
            this.texture = texture;
            this.bounds = bounds;
            activated = true;
        }

        #endregion

        #region Public Methods
        public virtual void Update()
        {
            if (activated)
            {
                TouchCollection touchCollection = TouchPanel.GetState();
                Vector2 touchPosition = Vector2.Zero;

                if (touchCollection.Count > 0)
                    touchPosition = touchCollection[0].Position;

                if (CheckIfFirstTouchDown(touchPosition))
                {
                    DoOnTouchDown();
                }
                else if (CheckIfTouchRelease(touchPosition))
                {
                    DoOnTouchRelease();
                }
            }
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (texture != null && activated)
                spriteBatch.Draw(texture, bounds, Color.White);
        }

        #endregion

        #region Private Methods
        protected bool CheckIfFirstTouchDown(Vector2 touchPosition)
        {
            return !pressed && Bounds.Contains((int)touchPosition.X, (int)touchPosition.Y);
        }

        protected bool CheckIfTouchRelease(Vector2 touchPosition)
        {
            return pressed && touchPosition == Vector2.Zero;
        }

        protected virtual void DoOnTouchDown()
        {
            pressed = true;
            OnTouchDown(EventArgs.Empty);
        }

        protected virtual void DoOnTouchRelease()
        {
            pressed = false;
            OnClick(EventArgs.Empty);
        }

        #endregion
    }
}
